package com.burgeon.backend.SHOP.service.Impl;

import com.burgeon.backend.SHOP.entity.Mail;
import com.burgeon.backend.SHOP.entity.Order;
import com.burgeon.backend.SHOP.mappers.MailMapper;
import com.burgeon.backend.SHOP.mappers.OrderMapper;
import com.burgeon.backend.SHOP.service.BasketService;
import com.burgeon.backend.SHOP.service.MailService;
import com.burgeon.backend.Util.KdniaoTrackQueryAPI;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MailServiceImpl implements MailService {
    private final static Log logger = LogFactory.getLog(MailService.class);
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MailMapper mailMapper;

    @Override
    public Mail GetMail(String id){
        return mailMapper.FindMailById(id);
    }

    @Override
    public List<String> FindExpress(String oid) {
        KdniaoTrackQueryAPI kdniaoTrackQueryAPI = new KdniaoTrackQueryAPI();
        Order order = orderMapper.FindOrderMail(oid);//获取物流
        List<Mail> mails = mailMapper.FindMailByOid(oid);
        System.err.println("mails =" +mails);
        List<String> data = new ArrayList<>();
        for (Mail mail:mails){
            try {
                data.add(kdniaoTrackQueryAPI.getOrderTracesByJson(mail.getName(),mail.getCode(),order.getPhone()));
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("物流信息错误");
                logger.error(e.getMessage());
                System.err.println("物流信息错误");
            }
        }
        return data;
    }

    @Override
    public List<Mail> GetMailType(String token) {
        return mailMapper.getMail(token);
    }
}
